//
//  TPAccountViewController.m
//  CloudAtCostPanel
//
//  Created by leonardo on 2017/1/26.
//  Copyright © 2017年 tanpengsccd. All rights reserved.
//

#import "TPAccountViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
@interface TPAccountViewController ()<UITableViewDelegate ,UITableViewDataSource>
PRO_S UITableView * tableView;
@end

@implementation TPAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Account";
    // Do any additional setup after loading the view.
    _tableView = [[UITableView alloc]init];
    _tableView.delegate = self;
    _tableView.dataSource = self ;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:_tableView];
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
//    if (cell) {
//        <#statements#>
//    }
    UITableViewCell * cell = [UITableViewCell new];
    switch (indexPath.row) {
        case 0:
        {
            TPLabel * label = [TPLabel labelWithText:@"Touch ID Login" Font:FONTSIZE_TITLE];
            [cell.contentView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(label.superview);
                make.left.mas_equalTo(label.superview).offset(MARGIN_WINDOW);
            }];
            
            UISwitch * touchIDswitch  = [[UISwitch alloc]init];
            [cell.contentView addSubview:touchIDswitch];
            bool enable = [[NSUserDefaults standardUserDefaults] boolForKey:TOUCHIDLOGIN];
            touchIDswitch.on = enable;
            [touchIDswitch addTarget:self action:@selector(tapSwitch:) forControlEvents:UIControlEventValueChanged];
            
            [touchIDswitch mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(touchIDswitch.superview);
                make.right.mas_equalTo(touchIDswitch.superview).offset(-MARGIN_WINDOW);
                //            make.size.mas_equalTo(CGSizeMake(PX(180), PX(80)));
            }];
        }
            break;
        case 1:
        {
            TPButton * btn_logout = [[TPButton alloc]initwithNomalBtnTitleStr:@"logout"];
            [cell.contentView addSubview:btn_logout];
            
            
            [btn_logout addTarget:self action:@selector(tapBtn_logout) forControlEvents:UIControlEventTouchUpInside];
            
            
            [btn_logout mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(btn_logout.superview);
            }];
        }
            
        default:
            break;
    }
    
    return cell;
}

-(void)tapSwitch:(UISwitch *) sender{
//    //指纹解锁
//    
//    
//    LAContext *laContext = [[LAContext alloc] init];
//    NSString  * willState = !sender.on?@"off":@"on";
//    //开启指纹登录 必须通过指纹验证，关闭 2者都可（登录密码，指纹）
//    laContext.localizedFallbackTitle = !sender.on?@"输入登录密码":@"";
//    
//    NSError *error;
//    
//    if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//        
//        //        NSString  * willState = !sender.on?@"关闭":@"开启";
//        NSString * str = [NSString stringWithFormat:@"Turning %@  require Touch ID authentication ",willState];
//        
//        [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
//                  localizedReason:str
//                            reply:^(BOOL success, NSError *error) {
//                                if (success) {
//                                    NSLog(@"Touch ID authentication is OK");
//                                    //do your work
//                                    bool state = [[NSUserDefaults standardUserDefaults] boolForKey:TOUCHIDLOGIN];
//                                    [[NSUserDefaults standardUserDefaults] setBool:!state forKey:TOUCHIDLOGIN];
//                                    
//                                    dispatch_async(dispatch_get_main_queue(), ^{
////                                        CustomAlertView * alert = [[CustomAlertView alloc]initWithTitle:[NSString stringWithFormat:@"Operation succeed！",willState]];
////                                        [alert show];
//                                        TPAlertViewController * alert = [[TPAlertViewController sharedInstanceWithIdentifier:NSStringFromClass(self.class)]createOkWithTitle:@"Operation succeed！" subtitle:nil];
////                                        [self presentViewController:alert animated:YES completion:nil];
//                                        [alert showViewWithAnimated:YES];
//                                        sender.on = [[NSUserDefaults standardUserDefaults] boolForKey:TOUCHIDLOGIN];
//                                        
//                                    });
//                                    
//                                }
//                                if (error) {
//                                    //                                    [[NSUserDefaults standardUserDefaults] setObject:@0 forKey:TOUCHIDLOGIN];
//                                    NSLog(@"---failed to evaluate---error: %@---", error.description);
//                                    if (error) {
//                                        //                                    [[NSUserDefaults standardUserDefaults] setObject:@0 forKey:TOUCHIDLOGIN];
//                                        
//                                        switch (error.code) {
//                                            case  LAErrorAuthenticationFailed:{
//                                                if(sender.on){
////                                                    [self showEnterLoginPWD];
//                                                }else{
//                                                    dispatch_async(dispatch_get_main_queue(), ^{
//                                                        TPAlertViewController * alert = [[TPAlertViewController sharedInstanceWithIdentifier:NSStringFromClass(self.class)]createOkWithTitle:@"Authentication was not successful" subtitle:nil];
////                                                        [self presentViewController:alert animated:YES completion:nil];
//                                                        [alert showViewWithAnimated:YES];
//                                                    });
//                                                    
//                                                }
//                                                
//                                                
//                                            }
//                                                break;
//                                                
//                                            case    LAErrorUserCancel:{
//                                                
//                                                
//                                            }
//                                                break;
//                                                
//                                            case   LAErrorUserFallback:{
////                                                [self showEnterLoginPWD];
//                                            }
//                                                break;
//                                                
//                                            case  LAErrorSystemCancel:{
//                                                
//                                            }
//                                                break;
//                                            case  LAErrorPasscodeNotSet:{
//                                                dispatch_async(dispatch_get_main_queue(), ^{
//                                                    TPAlertViewController * alert = [[TPAlertViewController sharedInstanceWithIdentifier:NSStringFromClass(self.class)]createOkWithTitle:@"passcode is not set on the device" subtitle:nil];
////                                                    [self presentViewController:alert animated:YES completion:nil];
//                                                    [alert showViewWithAnimated:YES];
//                                                    
//                                                });
//                                                
//                                                //
//                                            }
//                                                break;
//                                            case  LAErrorTouchIDNotAvailable:{
//                                                
//                                            }
//                                                break;
//                                            case  LAErrorTouchIDNotEnrolled:{
//                                                
//                                            }
//                                                break;
//                                            case  LAErrorTouchIDLockout:{
//                                                if(sender.on){
////                                                    [self showEnterLoginPWD];
//                                                }else{
//                                                    TPAlertViewController * alert = [[TPAlertViewController sharedInstanceWithIdentifier:NSStringFromClass(self.class)]createOkWithTitle:@"Authentication was not successful" subtitle:nil];
////                                                    [self presentViewController:alert animated:YES completion:nil];
//                                                    [alert showViewWithAnimated:YES];
//                                                }
//                                                
//                                                
//                                            }
//                                                break;
//                                            case  LAErrorAppCancel:{
//                                                
//                                            }
//                                                break;
//                                            case LAErrorInvalidContext:{
//                                                
//                                            }
//                                                break;
//                                                
//                                                
//                                            default:
//                                                break;
//                                        }
//                                        
//                                        
//                                        
//                                    }
//                                    
//                                    
//                                    //                                        sender.on = [[NSUserDefaults standardUserDefaults] boolForKey:TOUCHIDLOGIN];
//                                    
//                                    
//                                    //do your error handle
//                                }
//                            }];
//    }
//    else {
//        NSLog(@"==========Not support :%@", error.description);
//        
//    }
//    sender.on = [[NSUserDefaults standardUserDefaults] boolForKey:TOUCHIDLOGIN];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)tapBtn_logout{
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:KEY];
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_GO_LOGIN object:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
